package com.kaifamiao.jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.time.LocalDate;
import java.util.Arrays;

/**
 * 批量执行DML语句
 */
public class JdbcTest09 {

    public static void main(String[] args) throws SQLException {
        DbHelper dh = new DbHelper();
        dh.load();
        Connection c = dh.connect();

        /*
        Statement st = c.createStatement();
        st.addBatch( "INSERT INTO t_staff ( id , name ) VALUES ( 1001 , '姜禹志' )" );
        st.addBatch( "INSERT INTO t_staff ( id , name ) VALUES ( 2002 , '姜玉芝' )" );
        st.addBatch( "INSERT INTO t_staff ( id , name ) VALUES ( 3003 , '小姜' )" );
        // 将一批SQL发送到数据库服务器并获取它们的执行结果
        int[] counts = st.executeBatch();
        System.out.println(Arrays.toString( counts ) );
        dh.release( st );
        */

        String sql = "INSERT INTO t_staff ( id , name , gender , hiredate ) VALUES (?,?,?,?)" ;
        PreparedStatement ps = c.prepareStatement( sql );

        // 设置第一组参数占位符的值
        ps.setInt( 1 , 1001 );
        ps.setString( 2 , "姜禹志" );
        ps.setString( 3 , "靓仔" );
        ps.setDate( 4 , java.sql.Date.valueOf( LocalDate.of( 2021 , 12 , 20 ) ) );
        // 将第一组参数占位符的值添加到当前批次中
        ps.addBatch();

        // 设置第二组参数占位符的值
        ps.setInt( 1 , 2002 );
        ps.setString( 2 , "赖宇" );
        ps.setString( 3 , "靓仔" );
        ps.setDate( 4 , java.sql.Date.valueOf( LocalDate.of( 2021 , 12 , 19 ) ) );
        // 将第二组参数占位符的值添加到当前批次中
        ps.addBatch();

        // 设置第三组参数占位符的值
        ps.setInt( 1 , 3003 );
        ps.setString( 2 , "曾子豪" );
        ps.setString( 3 , "靓仔" );
        ps.setDate( 4 , java.sql.Date.valueOf( LocalDate.of( 2021 , 12 , 18 ) ) );
        // 将第三组参数占位符的值添加到当前批次中
        ps.addBatch();

        int[] counts = ps.executeBatch();
        System.out.println(Arrays.toString( counts ) );

        dh.release( ps );
        dh.release( c );

    }

}
